package com.distribute.framework.model;

import com.distribute.framework.core.web.helper.Utils;
import java.lang.reflect.Field;
import java.util.Date; 
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;

import javax.annotation.Resource;

import com.distribute.framework.core.web.util.Page;
import com.distribute.framework.core.lang.*; 
import com.distribute.framework.core.web.helper.WebHelper.LzMap;
import com.distribute.framework.core.util.Function;
import com.distribute.framework.core.util.IbatisMap;
import com.distribute.framework.config.*;
import com.distribute.framework.plugin.*;

@namespace("products/balancedetails")
public @caption("余额消费明细") @database(DatabaseNames.distribute_sys) @source("distribute_sys") @table("balance_details") class BalanceDetails implements java.io.Serializable
{
	private static final long serialVersionUID = 1l;
		
	
		
	private @caption("编号") @column("id")    @primary  @notnull  @note("  ") Long id;// 主键 非空     
			
 			
 			
 		
	
		
	private @caption("品牌") @column("brand")     @notnull  @relate("$brand") @RelateClass(Merchant.class)  @note("  ") Long brand;// 非空     
			
 	private transient Merchant $brand;//关联对象[品牌信息]
 			
 			
 			
 		
	
		
	private @caption("会员") @column("member")     @notnull  @relate("$member") @RelateClass(Member.class)  @note("  ") Long member;// 非空     
			
 	private transient Member $member;//关联对象[会员信息]
 			
 			
 			
 		
	
		
	private @caption("所属订单") @column("order")     @notnull  @relate("$order") @RelateClass(DirectOrder.class)  @note("  ") Long order;// 非空     
			
 	private transient DirectOrder $order;//关联对象[商品订单]
 			
 			
 			
 		
	
		
	private @caption("消费数量") @column("number")     @notnull  @note("  ") Integer number;// 非空     
			
 			
 			
 		
	
		
	private @caption("余额产品") @column("products")     @notnull  @relate("$products") @RelateClass(Product.class)  @note("  ") Long products;// 非空     
			
 	private transient Product $products;//关联对象[产品余额]
 			
 			
 			
 		
	
	 
	//默认构造函数
	public BalanceDetails(){ 
	
	}
	
	//主键构造函数
	public BalanceDetails(Long id){
		this.id = id;
	}
	
	/**所有字段构造函数 
	 BalanceDetails(
	 	Utils.getLong("")//品牌 [非空]
	 	,Utils.getLong("")//会员 [非空]
	 	,Utils.getLong("")//所属订单 [非空]
	 	,Utils.getInteger("")//消费数量 [非空]
	 	,Utils.getLong("")//余额产品 [非空]
	 )
	**/
	public BalanceDetails(Long brand,Long member,Long order,Integer number,Long products){
		
			
			
		this.brand=brand;
		
			
		this.member=member;
		
			
		this.order=order;
		
			
		this.number=number;
		
			
		this.products=products;
		
	}
	
	//设置非空字段
	public BalanceDetails setNotNull(Long brand,Long member,Long order,Integer number,Long products){
		
			
			
		this.brand=brand;
		
			
		this.member=member;
		
			
		this.order=order;
		
			
		this.number=number;
		
			
		this.products=products;
		
		return this;
	}
	
	
		
	/** 编号 [非空]       **/
	public BalanceDetails id(Long id){
		this.id = id;
		this.setSeted(F.id);
		return this;
	}
	
		
	/** 品牌 [非空] [Merchant]      **/
	public BalanceDetails brand(Long brand){
		this.brand = brand;
		this.setSeted(F.brand);
		return this;
	}
	
		
	/** 会员 [非空] [Member]      **/
	public BalanceDetails member(Long member){
		this.member = member;
		this.setSeted(F.member);
		return this;
	}
	
		
	/** 所属订单 [非空] [DirectOrder]      **/
	public BalanceDetails order(Long order){
		this.order = order;
		this.setSeted(F.order);
		return this;
	}
	
		
	/** 消费数量 [非空]       **/
	public BalanceDetails number(Integer number){
		this.number = number;
		this.setSeted(F.number);
		return this;
	}
	
		
	/** 余额产品 [非空] [Product]      **/
	public BalanceDetails products(Long products){
		this.products = products;
		this.setSeted(F.products);
		return this;
	}
	
	
	
	/**
	* 克隆对象
	**/
	public BalanceDetails clone(){
		BalanceDetails clone = new BalanceDetails();
		
			
			
		clone.brand=this.brand;
		
			
		clone.member=this.member;
		
			
		clone.order=this.order;
		
			
		clone.number=this.number;
		
			
		clone.products=this.products;
		
		return clone;
	}
	
	
	
	
	
	/**
	 * 根据ID取余额消费明细
	 * @param id
	 * @return
	 */
	public static @api BalanceDetails get(Long id){		
		return getBalanceDetailsById(id);
	}
	
	
	
	/**
	 * 获取所有余额消费明细
	 * @return
	 */
	public static @api List<BalanceDetails> list(Map params,Integer size){
		return getBalanceDetailsList(params,size);
	}
	/**
	 * 获取余额消费明细分页
	 * @return
	 */
	public static @api Page<BalanceDetails> page(Integer page,Integer size,Map params){
		return getBalanceDetailsPage(page, size , params);
	}
	
	
	/**
	 * 根据查询条件取余额消费明细
	 * @param params
	 * @return
	 */
	public static @api BalanceDetails Get(Map params){
		return getBalanceDetails(params);
	}
	/**
	 * 获取余额消费明细数
	 * @return
	 */
	public static @api Long count(Map params){
		return getBalanceDetailsCount(params);
	}
	/**
	 * 获取余额消费明细数
	 * @return
	 */
	public static @api <T> T eval(String eval,Map params){
		return getBalanceDetailsEval(eval,params);
	}
	
	
	/**
	 * 获取余额消费明细图表统计数据
	 * @param params 查询条件
	 * @param sumKey 统计的字段
	 * @param groupKey 1:按年， 2：按月， 3：按周， 4：按日， 5：按小时， 6：月分， 7：日分， 8：点分
	 * @return
	 */
	public static @api List<Map> stats(Map params,C sumKey,C groupKey){
		return getBalanceDetailsStats(params,sumKey,groupKey,-1);
	}
	/**
	 * 获取余额消费明细图表统计数据
	 * @param params 查询条件
	 * @param sumKey 统计的字段
	 * @param groupKey 分组的字段
	 * @param dateType 1:按年， 2：按月， 3：按周， 4：按日， 5：按小时， 6：月分， 7：日分， 8：点分
	 * @return
	 */
	public static @api List<Map> stats(Map params,C sumKey,C groupKey,int dateType){
		return getBalanceDetailsStats(params,sumKey,groupKey,dateType);
	}
	
	
	/**
	 * 根据ID取余额消费明细
	 * @param id
	 * @return
	 */
	public static @api BalanceDetails getBalanceDetailsById(Long id){		
		return getBalanceDetailsDao().getBalanceDetailsById(id);
	}
	
	
	
	
	/**
	 * 根据ID取余额消费明细的标识键值
	 * @param id
	 * @return
	 */
	public static @api String getKeyValue(Object key){
		Long id = Utils.parseLong(key);	
		if(id==null || id.longValue()==0 ){
			
			return null;
			
		}
		String value = null;
				
		if(value==null){
			BalanceDetails balanceDetails = get(id);
			if(balanceDetails!=null){
				String strValue = balanceDetails.getId$();
				
				
				value = strValue;
			}else{
				
				value = "{"+id+"}";
			}
		}
		
		/************LAZY3Q_ID_KEYVALUE************/
		/************LAZY3Q_ID_KEYVALUE************/
		return value;
	}	
	
	
	/**
	 * 获取当前对象的键值
	 * @return
	 */
	public String getKeyValue(){
		
		/************LAZY3Q_CODE_KEYVALUE************/
		/************LAZY3Q_CODE_KEYVALUE************/
		String keyValue = this.getId$();
		
		return keyValue;
	}
	
	
	/**
	 * 根据关键字精确查询余额消费明细对象
	 * @return
	 */
	public static @api BalanceDetails find(String key){
		if(Utils.empty(key))return null;
		Map params = new HashMap();
		if(key.toLowerCase().startsWith("id:"))//如果查询条件以id:开头，则按ID查询
			params.put("id", key.toLowerCase().replaceFirst("id:", ""));					
		else{//按标识查询，精确查询
			String strDefinex = "";
			
			strDefinex="(2=1 "+strDefinex+")";
			params.put("definex",strDefinex);
		}
		
		/************LAZY3Q_CODE_FIND************/
		/************LAZY3Q_CODE_FIND************/
		return BalanceDetails.getBalanceDetails(params);
	}
	
	
	/**
	 * 根据关键字查询余额消费明细
	 * @return
	 */
	public static @api List<Map> query(String key,int size,Map params,Boolean bObject){
		if(Utils.empty((String)params.get("asc")) && Utils.empty((String)params.get("desc")))//如果未传入排序字段
			params.put("desc","id");//那么，默认以主键降序，传给Dao层
		String strTerm=key;		
		if(!Utils.empty(strTerm)){
			if(strTerm.toLowerCase().startsWith("id:"))//如果查询条件以id:开头，则按ID查询
				params.put("id", strTerm.toLowerCase().replaceFirst("id:", ""));					
			else//按标识查询，模糊查询请带%
			{
				String strDefinex = "";
				
				strDefinex="(2=1 "+strDefinex+")";
				params.put("definex",strDefinex);
			}
		}
		
		
		/************LAZY3Q_CODE_QUERY************/
		/************LAZY3Q_CODE_QUERY************/
			
		List<BalanceDetails> list = page(0,size,params).getResult();
		
		/**
		* OK!取到数据拼成放入Map中，[{value,text,object:{...}},...]
		* value:数据ID，text:数据标识,object,整个对象
		**/
		List<Map> result = new java.util.ArrayList();
		for(BalanceDetails balanceDetails:list){
			Map map = new HashMap();
			map.put("key",balanceDetails.getId().toString());
			map.put("value", Utils.js(balanceDetails.getKeyValue()));
			if(bObject==true)
				map.put("object",balanceDetails);
			result.add(map);
		}
		
		
		/************LAZY3Q_AFTER_QUERY************/
		/************LAZY3Q_AFTER_QUERY************/
		
		return result;
	}
	
	
	
	/**
	 * 获取所有余额消费明细
	 * @return
	 */
	public static @api List<BalanceDetails> getBalanceDetailsList(Map params,Integer size){
		return getBalanceDetailsDao().getBalanceDetailsList(params,0,size);
	}
	
	/**
	 * 获取余额消费明细分页
	 * @return
	 */
	public static @api Page<BalanceDetails> getBalanceDetailsPage(Integer page,Integer size,Map params){
		if(page==null)page=0;
		if(size==null)size=20;
		return getBalanceDetailsDao().getBalanceDetailsPage(page, size , params);
	}
	
	/**
	 * 根据查询条件取余额消费明细
	 * @param params
	 * @return
	 */
	public static @api BalanceDetails getBalanceDetails(Map params){
		return getBalanceDetailsDao().getBalanceDetails(params);
	}
	
	/**
	 * 保存一个余额消费明细
	 * @param balanceDetails
	 * @return
	 */
	public static @api BalanceDetails saveBalanceDetails(BalanceDetails balanceDetails){
		return getBalanceDetailsDao().saveBalanceDetails(balanceDetails);
	}
	
	/**
	 * 根据条件更新余额消费明细
	 * @param values
	 * @param params
	 */
	public static @api int updateBalanceDetailsByConfirm(Map values, Map params){
		return getBalanceDetailsDao().updateBalanceDetailsByConfirm(values,params);
	}
	
	/**
	 * 修改一个余额消费明细
	 * @param balanceDetails
	 * @return
	 */
	public static @api int updateBalanceDetails(BalanceDetails balanceDetails){
		return getBalanceDetailsDao().updateBalanceDetails(balanceDetails);
	}
	
	/**
	 * 更新余额消费明细非空字段
	 * @param balanceDetails
	 */
	public static @api int updateBalanceDetails$NotNull(BalanceDetails balanceDetails){
		return getBalanceDetailsDao().updateBalanceDetails$NotNull(balanceDetails);
	}
	
	/**
	 * 获取余额消费明细数
	 * @return
	 */
	public static @api Long getBalanceDetailsCount(Map params){
		return getBalanceDetailsDao().getBalanceDetailsCount(params);
	}
		
		
	/**
	 * 获取余额消费明细自定义求和表达式,比如求和:eval="sum(id)"
	 * @return
	 */	
	public static @api <T> T getBalanceDetailsEval(String eval,Map params){
		return getBalanceDetailsDao().getBalanceDetailsEval(eval,params);
	}
	
	
	/**
	 * 获取余额消费明细图表统计数据
	 * @param params 查询条件
	 * @param sumKey 统计的字段
	 * @param groupKey 分组的字段
	 * @param dateType 1:按年， 2：按月， 3：按周， 4：按日， 5：按小时， 6：月分， 7：日分， 8：点分
	 * @return
	 */
	public static @api List<Map> getBalanceDetailsStats(Map params,C sumKey,C groupKey,int dateType){
		//TODO:图表统计功能未实现
		
		Map<String,Object> groups=new HashMap();//要分组的字段
		Map<String,Object> sums=new HashMap();//要统计的字段
		
		String[][] dateFormats=new String[][]{
				null,
				new String[]{"%Y","yyyy年","Y"},
				new String[]{"%Y-%m","yyyy年MM月","M"},
				new String[]{"%Y%U","yyyy年第ww周","W"},
				new String[]{"%Y-%m-%d","yyyy年MM月dd日","D"},
				new String[]{"%Y-%m-%d %H","yyyy年MM月dd日 HH点","H"},
				new String[]{"%m","MM月"},
				new String[]{"%d","dd号"},
				new String[]{"%H","HH点"} 
		};
		
		//获取要分组的字段，放入Map中
		if(dateType>0){
			groups.put(groupKey.toString(),"DATE_FORMAT({key},'"+dateFormats[dateType][0]+"')");
		}else{
			groups.put(groupKey.toString(),"{key}");
		}
		if(dateType<6 && dateType>0){ 
			params.put("asc", groupKey.toString()); 
			if(params.get(groupKey.name()+"Start")==null){
				params.put(groupKey.name()+"Start", Utils.date(new Date(), -24, dateFormats[dateType][2]));
			}
		}
		
		
		//获取要统计的字段，放入Map中
		if(sumKey.toString().equals("count")){
			sums.put(sumKey.toString(),"count(*)");
		}else{
			sums.put(sumKey.toString(),"sum({key})");
		}
		
		List<Map> list = getBalanceDetailsDao().getBalanceDetailsStats(params,groups,sums);
		
		List<Map> result = new ArrayList();
		for(Map m:list){
			Map map = new HashMap();
			Object value = m.get(sumKey.toString());
			Object name = null;
			Object groupValue = m.get(groupKey.toString());
			if(dateType>0){
				name = Utils.date(Utils.parseDate(groupValue), dateFormats[dateType][1]);
			}else{
				try{
					java.lang.reflect.Method method = BalanceDetails.class.getMethod("get"+Utils.ForMat(groupKey.toString())+"$");
					method.setAccessible(true);
					name = method.invoke(BalanceDetails.fromMap(m));
				}catch(Exception ex){
					ex.printStackTrace();
				}
			}
			map.put("name", name);
			map.put("value", value);
			result.add(map);
		}
		return result;
	}
	
	
	/**
	 * 根据条件更新数据
	 * @param set
	 * @param where
	 */
	public static int updateBalanceDetails(Map set,Map where){
		if(set.isEmpty())throw new RuntimeException("set为空");
		if(where.isEmpty())throw new RuntimeException("where为空");
		where.put("confirm", "1");
		return getBalanceDetailsDao().updateBalanceDetailsByConfirm(set, where);
	}
	
	
	/**
	 * 根据条件更新数据
	 * @param set
	 * @param where
	 */
	public static void deleteBalanceDetails(Map where){
		if(where.isEmpty())throw new RuntimeException("where为空");
		where.put("confirm", "1");
		getBalanceDetailsDao().deleteBalanceDetailsByConfirm(where);
	}
	
	/**
	 * 根据条件更新数据
	 * @param set
	 * @param where
	 */
	public static void delete(Long id){
		if(id==null)throw new RuntimeException("id为空");
		
		getBalanceDetailsDao().deleteBalanceDetailsById(id);
		
	}
	
	
	/**
	 * 保存余额消费明细对象
	 * @param params
	 * @return
	 */
	public BalanceDetails save(){
		if(this.getId()!=null)
			getBalanceDetailsDao().updateBalanceDetails(this);
		else
			return getBalanceDetailsDao().saveBalanceDetails(this);
		return this;
	}
	
	
	/**
	 * 更新余额消费明细对象的非空字段,注意跟Service里面的update不一样哦
	 * @param params
	 * @return
	 */
	public int update(){
		return getBalanceDetailsDao().updateBalanceDetails$NotNull(this);
	}
	
	
	/**
	 * 删除
	 * @param params
	 * @return
	 */
	public void delete(){
		
			getBalanceDetailsDao().deleteBalanceDetailsById(this.getId());
	}
	
	/**
	 * 执行事务
	 * @param function
	 */
	public static <T> T execute(Function function){
		return getBalanceDetailsDao().executeTransaction(function);
	}

	
	
	/*******************************编号**********************************/	
	/**
	* 编号 [非空]      
	**/
	public Long getId(){
		
		if(this.id!=null){
			return this.id;
		}
		
		return this.id;
	}
	/**
	* 获取编号格式化(toString)
	**/
	public String getId$(){
		String strValue="";
	
	
		 strValue=Utils.string(this.getId());
	 
	 	 return strValue;
	}
	/**
	* 根据编号反格式化
	**/
	public void setId$(String strValue){
		if(Utils.empty(strValue)){
			this.id=null;
			return;
		}
	
	
	 	this.id=Utils.parseLong(strValue);
	 
	}
	
	/**
	* 编号 [非空]      
	**/
	public void setId(Long id){
		this.id = id;
		
		this.setSeted(F.id);
		
	}
	
	
	
	
	
	
	
	
	
	
 	
 	
	
	
	
	
	
 	
 	
 	
	
	
	/*******************************品牌**********************************/	
	/**
	* 品牌 [非空] [Merchant]     
	**/
	public Long getBrand(){
		
		if(this.brand!=null){
			return this.brand;
		}
		
		return this.brand;
	}
	/**
	* 获取品牌格式化(toString)
	**/
	public String getBrand$(){
		String strValue="";
	
	
		 
		if(this.getBrand()!=null){
			
				strValue+=Utils.string(Merchant.getKeyValue(this.getBrand()));
			
		}			
		 
	 
	 	 return strValue;
	}
	/**
	* 根据品牌反格式化
	**/
	public void setBrand$(String strValue){
		if(Utils.empty(strValue)){
			this.brand=null;
			return;
		}
	
	
		 
		Merchant item = Merchant.find(strValue);
		if(item!=null){
			
			this.brand = Utils.parseLong(item.getId());
			
		}
		 
	 
	}
	
	/**
	* 品牌 [非空] [Merchant]     
	**/
	public void setBrand(Long brand){
		this.brand = brand;
		
		this.setSeted(F.brand);
		
	}
	
	
		 
	
	
	
	
	
	
	
	
	
 	
 	
	
	
	
	
	
 	
 		
	/**
	* 获取关联对象[品牌信息]
	**/	 			
 	public Merchant get$brand(){
 		if(this.$brand!=null)
 			return this.$brand;
 		com.distribute.framework.model.Merchant merchant = null;
		if(this.getBrand()!=null){
		
 			merchant = Merchant.get(this.getBrand());
 		
 		}
 		return merchant;
	}
	
 	
	
	
	/*******************************会员**********************************/	
	/**
	* 会员 [非空] [Member]     
	**/
	public Long getMember(){
		
		if(this.member!=null){
			return this.member;
		}
		
		return this.member;
	}
	/**
	* 获取会员格式化(toString)
	**/
	public String getMember$(){
		String strValue="";
	
	
		 
		if(this.getMember()!=null){
			
				strValue+=Utils.string(Member.getKeyValue(this.getMember()));
			
		}			
		 
	 
	 	 return strValue;
	}
	/**
	* 根据会员反格式化
	**/
	public void setMember$(String strValue){
		if(Utils.empty(strValue)){
			this.member=null;
			return;
		}
	
	
		 
		Member item = Member.find(strValue);
		if(item!=null){
			
			this.member = Utils.parseLong(item.getUid());
			
		}
		 
	 
	}
	
	/**
	* 会员 [非空] [Member]     
	**/
	public void setMember(Long member){
		this.member = member;
		
		this.setSeted(F.member);
		
	}
	
	
		 
	
	
	
	
	
	
	
	
	
 	
 	
	
	
	
	
	
 	
 		
	/**
	* 获取关联对象[会员信息]
	**/	 			
 	public Member get$member(){
 		if(this.$member!=null)
 			return this.$member;
 		com.distribute.framework.model.Member member = null;
		if(this.getMember()!=null){
		
 			member = Member.get(this.getMember());
 		
 		}
 		return member;
	}
	
 	
	
	
	/*******************************所属订单**********************************/	
	/**
	* 所属订单 [非空] [DirectOrder]     
	**/
	public Long getOrder(){
		
		if(this.order!=null){
			return this.order;
		}
		
		return this.order;
	}
	/**
	* 获取所属订单格式化(toString)
	**/
	public String getOrder$(){
		String strValue="";
	
	
		 
		if(this.getOrder()!=null){
			
				strValue+=Utils.string(DirectOrder.getKeyValue(this.getOrder()));
			
		}			
		 
	 
	 	 return strValue;
	}
	/**
	* 根据所属订单反格式化
	**/
	public void setOrder$(String strValue){
		if(Utils.empty(strValue)){
			this.order=null;
			return;
		}
	
	
		 
		DirectOrder item = DirectOrder.find(strValue);
		if(item!=null){
			
			this.order = Utils.parseLong(item.getId());
			
		}
		 
	 
	}
	
	/**
	* 所属订单 [非空] [DirectOrder]     
	**/
	public void setOrder(Long order){
		this.order = order;
		
		this.setSeted(F.order);
		
	}
	
	
		 
	
	
	
	
	
	
	
	
	
 	
 	
	
	
	
	
	
 	
 		
	/**
	* 获取关联对象[商品订单]
	**/	 			
 	public DirectOrder get$order(){
 		if(this.$order!=null)
 			return this.$order;
 		com.distribute.framework.model.DirectOrder directOrder = null;
		if(this.getOrder()!=null){
		
 			directOrder = DirectOrder.get(this.getOrder());
 		
 		}
 		return directOrder;
	}
	
 	
	
	
	/*******************************消费数量**********************************/	
	/**
	* 消费数量 [非空]      
	**/
	public Integer getNumber(){
		
		if(this.number!=null){
			return this.number;
		}
		
		return this.number;
	}
	/**
	* 获取消费数量格式化(toString)
	**/
	public String getNumber$(){
		String strValue="";
	
	
		 strValue=Utils.string(this.getNumber());
	 
	 	 return strValue;
	}
	/**
	* 根据消费数量反格式化
	**/
	public void setNumber$(String strValue){
		if(Utils.empty(strValue)){
			this.number=null;
			return;
		}
	
	
	 	this.number=Utils.parseInteger(strValue);
	 
	}
	
	/**
	* 消费数量 [非空]      
	**/
	public void setNumber(Integer number){
		this.number = number;
		
		this.setSeted(F.number);
		
	}
	
	
	
	
	
	
	
	
	
	
 	
 	
	
	
	
	
	
 	
 	
 	
	
	
	/*******************************余额产品**********************************/	
	/**
	* 余额产品 [非空] [Product]     
	**/
	public Long getProducts(){
		
		if(this.products!=null){
			return this.products;
		}
		
		return this.products;
	}
	/**
	* 获取余额产品格式化(toString)
	**/
	public String getProducts$(){
		String strValue="";
	
	
		 
		if(this.getProducts()!=null){
			
				strValue+=Utils.string(Product.getKeyValue(this.getProducts()));
			
		}			
		 
	 
	 	 return strValue;
	}
	/**
	* 根据余额产品反格式化
	**/
	public void setProducts$(String strValue){
		if(Utils.empty(strValue)){
			this.products=null;
			return;
		}
	
	
		 
		Product item = Product.find(strValue);
		if(item!=null){
			
			this.products = Utils.parseLong(item.getId());
			
		}
		 
	 
	}
	
	/**
	* 余额产品 [非空] [Product]     
	**/
	public void setProducts(Long products){
		this.products = products;
		
		this.setSeted(F.products);
		
	}
	
	
		 
	
	
	
	
	
	
	
	
	
 	
 	
	
	
	
	
	
 	
 		
	/**
	* 获取关联对象[产品余额]
	**/	 			
 	public Product get$products(){
 		if(this.$products!=null)
 			return this.$products;
 		com.distribute.framework.model.Product product = null;
		if(this.getProducts()!=null){
		
 			product = Product.get(this.getProducts());
 		
 		}
 		return product;
	}
	
 	
	
	
	
	/************LAZY3Q_DEFINE_CODE************/
	/************LAZY3Q_DEFINE_CODE************/	
	
	
	/**
	 * 触发器
	 */
	public static void on(Trigger.Index index,final Notify.Listener listener,Trigger.Type... types){
		Notify.on(BalanceDetails.class, index, listener, types);
	}
	public static void on(final Notify.Listener listener,String type){
		Notify.on(BalanceDetails.class, listener, type);
	}
	/**
	 * 当修改字段时
	 * @param filedName
	 * @param listener
	 */
	public static void on(String filedName,final Notify.Listener listener){
		Notify.on(BalanceDetails.class, listener, filedName);
	}
	/**
	 * 当修改字段为某值时
	 * @param filedName
	 * @param value
	 * @param listener
	 */
	public static void on(String filedName,Object value,final Notify.Listener listener){
		if(value==null)return;
		Notify.on(BalanceDetails.class, listener, filedName+"."+Utils.md5(""+value));
	}
	public void notify(String type){
		Trigger.on(BalanceDetails.class,type, this);
	}
	
	
	/**
	 * 用来获取当有字段关联对象时的获取方式,调用如${BalanceDetails.obj.filedName}
	 * 因为有些变态的模板语言不能识别$，所以不能以${BalanceDetails.$filedName}获取关联对象
	 */
	public Map getObj(){
		return new HashMap(){
			@Override
			public Object get(Object key) {
				try {
					return BalanceDetails.class.getMethod("get$"+Utils.forMat(key.toString())).invoke(BalanceDetails.this);
				} catch (Exception e) {
					e.printStackTrace();
				}
				return null;
			}
		};
	}
	/**
	 * 用来获取某字段格式化的获取方式,调用如${BalanceDetails.fmt.filedName}
	 * 因为有些变态的模板语言不能识别$，所以不能以${BalanceDetails.filedName$}获取某字段格式化
	 */
	public Map getFmt(){
		return new HashMap(){
			@Override
			public Object get(Object key) {
				try {
					return BalanceDetails.class.getMethod("get"+Utils.ForMat(key.toString()+"$")).invoke(BalanceDetails.this);
				} catch (Exception e) {
					e.printStackTrace();
				}
				return null;
			}
		};
	}
	/**
	 * 获取查询参数链式串
	 * @return
	 */
	public static @Deprecated M where(){ return new M(0); }
	public static @Deprecated M m(){ return new M(0); }
	public static M where = new M(0,true);
	public static @database(DatabaseNames.distribute_sys) class M extends IbatisMap{
		int type = 0; 
		M linked = null;
		Boolean proxy = false;
		public M set = null;
		public M(int type){
			this.type = type;
			if(this.type==0){
				set = new M(1,true);
				set.linked = this;
			}
		};
		public M(int type,Boolean proxy){
			this.type = type;
			this.proxy=proxy;
		};
		public M proxy(){
			M m = new M(this.type);//当前的对像
			if(this.type==0){
				m.set = new M(1,true);//set对象
				m.set.linked = m;
			}else{
				m.linked=this.linked;
				this.linked.linked=m;
			}
			return m;
		}
		public @Deprecated M set(){			
			M m = this;
			m.linked=new M(1);
			m.linked.linked=m;
			return m.linked;
		};
		
		public M on(IbatisMap value,C column,String joinColumn){if(proxy)return proxy().on(value,column,joinColumn);this.put(value.getClass().getDeclaringClass().getSimpleName(), value);value.put("on", column.toString());value.put("to", joinColumn);return this;};
		
		
			
		
		/** 编号 [非空]       **/
		public M id(Object id){if(proxy)return proxy().id(id);  this.put("id", id);return this;};
	 	/** and id is null */
 		public M idNull(){if(proxy)return proxy().idNull();if(this.get("idNot")==null)this.put("idNot", "");this.put("id", null);return this;};
 		/** not .... */
 		public M idNot(){if(proxy)return proxy().idNot();this.put("idNot", "not");return this;};
 		public M idNotNull(){if(proxy)return proxy().idNotNull();this.put("idNot", "not");this.put("id", null);return this;};
 		public M idNot(Object id){if(proxy)return proxy().idNot(id);this.put("idNot", "not");this.put("id", id);return this;};
	 		
		/** and id <= ? */
		public M idMin(Object min){if(proxy)return proxy().idMin(min);this.put("idMin", min);return this;};
		/** and id >= ? */
		public M idMax(Object max){if(proxy)return proxy().idMax(max);this.put("idMax", max);return this;};
			
			
		public M idIn(String ids){
			if(proxy)return proxy().idIn(ids);  this.put("idIn", ids);return this;
		};	
 			
		
			
	 	
			
		public M brand$on(Merchant.M value){if(proxy)return proxy().brand$on(value);this.put("Brand", value);value.put("on", "brand");return this;};
 				
 			
		
		/** 品牌 [非空] [Merchant]      **/
		public M brand(Object brand){if(proxy)return proxy().brand(brand);  this.put("brand", brand);return this;};
	 	/** and brand is null */
 		public M brandNull(){if(proxy)return proxy().brandNull();if(this.get("brandNot")==null)this.put("brandNot", "");this.put("brand", null);return this;};
 		/** not .... */
 		public M brandNot(){if(proxy)return proxy().brandNot();this.put("brandNot", "not");return this;};
 		public M brandNotNull(){if(proxy)return proxy().brandNotNull();this.put("brandNot", "not");this.put("brand", null);return this;};
 		public M brandNot(Object brand){if(proxy)return proxy().brandNot(brand);this.put("brandNot", "not");this.put("brand", brand);return this;};
	 		
			
		public M brandIn(String ids){
			if(proxy)return proxy().brandIn(ids);  this.put("brandIn", ids);return this;
		};	
 			
		
			
	 	
			
		public M member$on(Member.M value){if(proxy)return proxy().member$on(value);this.put("Member", value);value.put("on", "member");return this;};
 				
 			
		
		/** 会员 [非空] [Member]      **/
		public M member(Object member){if(proxy)return proxy().member(member);  this.put("member", member);return this;};
	 	/** and member is null */
 		public M memberNull(){if(proxy)return proxy().memberNull();if(this.get("memberNot")==null)this.put("memberNot", "");this.put("member", null);return this;};
 		/** not .... */
 		public M memberNot(){if(proxy)return proxy().memberNot();this.put("memberNot", "not");return this;};
 		public M memberNotNull(){if(proxy)return proxy().memberNotNull();this.put("memberNot", "not");this.put("member", null);return this;};
 		public M memberNot(Object member){if(proxy)return proxy().memberNot(member);this.put("memberNot", "not");this.put("member", member);return this;};
	 		
			
		public M memberIn(String ids){
			if(proxy)return proxy().memberIn(ids);  this.put("memberIn", ids);return this;
		};	
 			
		
			
	 	
			
		public M order$on(DirectOrder.M value){if(proxy)return proxy().order$on(value);this.put("Order", value);value.put("on", "order");return this;};
 				
 			
		
		/** 所属订单 [非空] [DirectOrder]      **/
		public M order(Object order){if(proxy)return proxy().order(order);  this.put("order", order);return this;};
	 	/** and order is null */
 		public M orderNull(){if(proxy)return proxy().orderNull();if(this.get("orderNot")==null)this.put("orderNot", "");this.put("order", null);return this;};
 		/** not .... */
 		public M orderNot(){if(proxy)return proxy().orderNot();this.put("orderNot", "not");return this;};
 		public M orderNotNull(){if(proxy)return proxy().orderNotNull();this.put("orderNot", "not");this.put("order", null);return this;};
 		public M orderNot(Object order){if(proxy)return proxy().orderNot(order);this.put("orderNot", "not");this.put("order", order);return this;};
	 		
			
		public M orderIn(String ids){
			if(proxy)return proxy().orderIn(ids);  this.put("orderIn", ids);return this;
		};	
 			
		
			
	 	
			
		
		/** 消费数量 [非空]       **/
		public M number(Object number){if(proxy)return proxy().number(number);  this.put("number", number);return this;};
	 	/** and number is null */
 		public M numberNull(){if(proxy)return proxy().numberNull();if(this.get("numberNot")==null)this.put("numberNot", "");this.put("number", null);return this;};
 		/** not .... */
 		public M numberNot(){if(proxy)return proxy().numberNot();this.put("numberNot", "not");return this;};
 		public M numberNotNull(){if(proxy)return proxy().numberNotNull();this.put("numberNot", "not");this.put("number", null);return this;};
 		public M numberNot(Object number){if(proxy)return proxy().numberNot(number);this.put("numberNot", "not");this.put("number", number);return this;};
	 		
		/** and number <= ? */
		public M numberMin(Object min){if(proxy)return proxy().numberMin(min);this.put("numberMin", min);return this;};
		/** and number >= ? */
		public M numberMax(Object max){if(proxy)return proxy().numberMax(max);this.put("numberMax", max);return this;};
			
			
		public M numberIn(String ids){
			if(proxy)return proxy().numberIn(ids);  this.put("numberIn", ids);return this;
		};	
 			
		
			
	 	
			
		public M products$on(Product.M value){if(proxy)return proxy().products$on(value);this.put("Products", value);value.put("on", "products");return this;};
 				
 			
		
		/** 余额产品 [非空] [Product]      **/
		public M products(Object products){if(proxy)return proxy().products(products);  this.put("products", products);return this;};
	 	/** and products is null */
 		public M productsNull(){if(proxy)return proxy().productsNull();if(this.get("productsNot")==null)this.put("productsNot", "");this.put("products", null);return this;};
 		/** not .... */
 		public M productsNot(){if(proxy)return proxy().productsNot();this.put("productsNot", "not");return this;};
 		public M productsNotNull(){if(proxy)return proxy().productsNotNull();this.put("productsNot", "not");this.put("products", null);return this;};
 		public M productsNot(Object products){if(proxy)return proxy().productsNot(products);this.put("productsNot", "not");this.put("products", products);return this;};
	 		
			
		public M productsIn(String ids){
			if(proxy)return proxy().productsIn(ids);  this.put("productsIn", ids);return this;
		};	
 			
		
			
	 	
	 	public M add(String key, Object value) {if(proxy)return proxy().add(key,value);this.put(key, value);return this;}
	 	public M add(F key, Object value) {if(proxy)return proxy().add(key,value);this.put(key.toString(), value);return this;}
	 	
	 	public M desc(C key) {if(proxy)return proxy().desc(key);this.put("desc", key.column);return this;}
	 	public M asc(C key) {if(proxy)return proxy().asc(key);this.put("asc", key.column);return this;}
	 	public M desc(String sql) {if(proxy)return proxy().desc(sql);this.put("desc", sql);return this;}
	 	public M asc(String sql) {if(proxy)return proxy().asc(sql);this.put("asc", sql);return this;}
	 	public M definex(String sql) {if(proxy)return proxy().definex(sql);this.put("definex", sql);return this;}
	 	
	 	/** 获取所有余额消费明细 **/
		public @api List<BalanceDetails> list(Integer size){			
			return getBalanceDetailsList(this,size);
		}
		/** 获取余额消费明细分页 **/
		public @api Page<BalanceDetails> page(Integer page,Integer size){			
			return getBalanceDetailsPage(page, size , this);
		}
		/** 根据查询条件取余额消费明细 **/
		public @api BalanceDetails get(){			
			return getBalanceDetails(this);
		}
		/** 获取余额消费明细数 **/
		public @api Long count(){			
			return getBalanceDetailsCount(this);
		}
		/** 获取余额消费明细表达式 **/
		public @api <T> T eval(String strEval){			
			return getBalanceDetailsEval(strEval,this);
		}
		
		/** 获取余额消费明细统计 **/
		public @api List<Map> stats(C sumKey,C groupKey,int dateType){			
			return getBalanceDetailsStats(this,sumKey,groupKey,dateType);
		}
		/** 获取余额消费明细统计 **/
		public @api List<Map> stats(C sumKey,C groupKey){			
			return getBalanceDetailsStats(this,sumKey,groupKey,-1);
		}
		
		/** 根据条件更新 **/
		public @api int update(){			
			M where = null;
			M set = null;
			if(this.type==0){
				if(this.linked==null)throw new RuntimeException("未发set");
				where = this;
				set = this.linked;
			}else{
				if(this.linked==null)throw new RuntimeException("未发where条件");
				set = this;
				where = this.linked;
			}
			return updateBalanceDetails(set,where);
		}
		/** 根据条件删除 **/
		public @api void delete(){			
			deleteBalanceDetails(this);
		}
	}
	
	public static class Param extends M{
		public Param(int type) {
			super(type);
		}
		public Param(int type, Boolean proxy) {
			super(type, proxy);
		}
	}
	
	/** 对象的字段描述 **/
	public static class F{
		
			
		/** 编号 [非空]       **/
		public final static @type(Long.class)  String id="id";
	 			
			
	 	
			
		/** 品牌 [非空] [Merchant]      **/
		public final static @type(Long.class)  String brand="brand";
	 			
			
	 	
			
		/** 会员 [非空] [Member]      **/
		public final static @type(Long.class)  String member="member";
	 			
			
	 	
			
		/** 所属订单 [非空] [DirectOrder]      **/
		public final static @type(Long.class)  String order="order";
	 			
			
	 	
			
		/** 消费数量 [非空]       **/
		public final static @type(Integer.class)  String number="number";
	 			
		/** and number >= ? */
		public final static @type(Integer.class) String numberMin="numberMin";
		/** and number <= ? */
		public final static @type(Integer.class) String numberMax="numberMax";
				
			
	 	
			
		/** 余额产品 [非空] [Product]      **/
		public final static @type(Long.class)  String products="products";
	 			
			
	 	
	 	public static String get(String name){
			try {
				Field field = F.class.getField(name);
				if(field!=null)
					return field.get(null).toString();
			} catch (Exception e) {
				//e.printStackTrace();
			}
			return null;
		}
	}
	
	
	
	/** 对象的数据库字段描述 **/
	public static enum C{
		
		id("id"),
		brand("brand"),
		member("member"),
		order("order"),
		number("number"),
		products("products"),
		count("count");
		String column;
		C(String column){
			this.column = column;
		}
		public String toString(){
			return this.column;
		}
		public static String get(String name){
			try {
				Field field = C.class.getField(name);
				if(field!=null)
					return field.get(null).toString();
			} catch (Exception e) {
				//e.printStackTrace();
			}
			return null;
		}
	}
	
	
	
	
	/**
	* 获取余额消费明细数据
	**/
	public static Object getData(Object param){
		if(param==null)
			return null;
		Object value = null;
		if(param.getClass()==Long.class)
			value = BalanceDetails.get((Long)param);
		else if(param instanceof java.util.Map){
			java.util.Map params = (Map) param;
			if(params.get("size")==null)
				value = BalanceDetails.getBalanceDetails(params);
			else
				value = BalanceDetails.getBalanceDetailsList(params, (Integer) params.get("size"));
		}else if(param.getClass()==Long.class )
			value =  BalanceDetails.where().id(param).get();
		else if(!Utils.empty(param.toString()))
			value = BalanceDetails.get(Long.valueOf(param.toString()));
		return value;
	}
	
	/**对象转json**/
	public Map toJSON(){
		return toJSON(false);
	}
	public Map toJSON(boolean simple){
		return toJSON(simple,0);
	}
	public Map toJSON(boolean simple,int step){
		Map map = new HashMap();
		map.put("id",this.getId());
	
		Utils.fms(map,simple?"":"id",this.getId(),this.getId$());
		
			
 		
	
		Utils.fms(map,simple?"":"brand",this.getBrand(),this.getBrand$());
		
			
 		
	
		Utils.fms(map,simple?"":"member",this.getMember(),this.getMember$());
		
			
 		
	
		Utils.fms(map,simple?"":"order",this.getOrder(),this.getOrder$());
		
			
 		
	
		Utils.fms(map,simple?"":"number",this.getNumber(),this.getNumber$());
		
			
 		
	
		Utils.fms(map,simple?"":"products",this.getProducts(),this.getProducts$());
		
			
 		
	
		if(this.suffix!=null)map.put("suffix", this.suffix);
		
		/************LAZY3Q_DEFINE_TOJSON************/
		/************LAZY3Q_DEFINE_TOJSON************/
		return map;
	}
	/**对象转map**/
	public Map toMap(){
		Map map = new HashMap();
	
		map.put("id",this.getId());
 		
	
		map.put("brand",this.getBrand());
 		
	
		map.put("member",this.getMember());
 		
	
		map.put("order",this.getOrder());
 		
	
		map.put("number",this.getNumber());
 		
	
		map.put("products",this.getProducts());
 		
	
		map.put("suffix", this.suffix);
		
		/************LAZY3Q_DEFINE_TOMAP************/
		/************LAZY3Q_DEFINE_TOMAP************/
		return map;
	}
	/**map转对象**/
	public static BalanceDetails fromMap(Map map){
		BalanceDetails balanceDetails = new BalanceDetails();
	
		balanceDetails.id = Utils.parseLong(map.get("id"));
 		
	
		balanceDetails.brand = Utils.parseLong(map.get("brand"));
 		
	
		balanceDetails.member = Utils.parseLong(map.get("member"));
 		
	
		balanceDetails.order = Utils.parseLong(map.get("order"));
 		
	
		balanceDetails.number = Utils.parseInteger(map.get("number"));
 		
	
		balanceDetails.products = Utils.parseLong(map.get("products"));
 		
	
		return balanceDetails;
	}
	
	
	/**字段设置标记队列**/
	private Map<String,Boolean> seted=new HashMap();
	public Boolean getSeted(String key){
		return seted.get(key);
	}
	public void setSeted(String key){
		seted.put(key,true);
	}
	/**同步未set的字段**/
	public void mergeSet(BalanceDetails old){
		Field[] fields = this.getClass().getDeclaredFields();
		try {
			for(Field field:fields){
				String key = field.getName();
				if(seted.get(key)==null && field.getAnnotation(column.class)!=null){
					field.setAccessible(true);
					field.set(this, field.get(old));					
				}
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
		
	/**自定义变量存储**/
	private Map values=new HashMap();
	public Map getValues(){
		return values;
	}
	public void setValues(String name,Object value){
		values.put(name,value);
	}
	
	/**分表时的表名后缀**/
	private String suffix;
	public String getSuffix() {
		return suffix;
	}
	public void setSuffix(String suffix) {
		this.suffix = suffix;
	}
	
	
	/**获取数据库名称**/
	public static String getDataBaseName(){
		return Utils.config("spring.datasource.distribute_sys.name");
	}
	
	
	public static String getIcon(){
		return "/images/icons/Wallet2.png";
	}
	public static String getIconfont(){
		return "icon-qianbao1";
	}
	public static enum ACT{
		列表,添加,修改,详情
	};
	public static String getUrl(ACT act,BalanceDetails.M params){
		if(params==null)params=new BalanceDetails.M(0);
		params.remove("joins");
		params.remove("definexs");
		params.remove("distinct");
		String strUrl = "/admin/products/balancedetails.edit.html?";
		if(params.get("id")!=null){
			strUrl+="&id="+Utils.encode(""+params.get("id"),Utils.UTF8);
		}
		if(act==ACT.列表){
			strUrl = "/admin/products/balancedetails.html?1=1";
		
		}else if(act==ACT.添加){
			strUrl+="&edittype=insert";
		
		
		}else if(act==ACT.修改){
			strUrl+="&edittype=update";
		
		}else if(act==ACT.详情){
			strUrl+="&edittype=detail";
		
		}		
		if(params!=null){
			for(Object key:params.keySet()){
				strUrl+="&"+key+"="+Utils.encode(""+params.get(key),Utils.UTF8)+"";
			}
		}
		return strUrl;
	}
	public String getDetailUrl(){
		return this.getUrl(ACT.详情, where.id(this.getId()));
	}
	
	/**
	* 转跳到当前地址
	**/
	public static Protocol redirect(BalanceDetails.ACT act,BalanceDetails.M params){
		Protocol protocol = Protocol.create(true,null,"ACT_REDIRECT");
		protocol.set("url",BalanceDetails.getUrl(act, params));
		return protocol;
	}
	/**
	* 打开一个新的tab页面
	**/
	public static Protocol href(BalanceDetails.ACT act,BalanceDetails.M params){
		Protocol protocol = Protocol.create(true,null,"ACT_HREF");
		protocol.set("url",BalanceDetails.getUrl(act, params));
		protocol.set("title",act.name());
		return protocol;
	}
	/**
	* 显示一个消息框，并且关闭当前的对话框
	**/
	public static Protocol message(String message){
		Protocol protocol = Protocol.create(true,null,"ACT_MESSAGE");
		protocol.set("message",message);
		return protocol;
	}
	
	
	static com.distribute.framework.dao.IBalanceDetailsDao getBalanceDetailsDao(){
		return Utils.getBean("balanceDetailsDao");
	};
	
}